#!/usr/bin/env python
import gtk
import gobject
import gtkhtml2
import webbrowser
import dieu.gui.add, dieu.gui.prefs, dieu.gui.about
import dieu.bookmark, dieu.database, dieu.settings, dieu.util, dieu.inet

class DieuBrowser:
    def __init__(self, username, password):
        self.page = "<html>\
  <body>\
      <b>Imagine that the title is here</b><br />\
  <i><a href='http://example.com'>http://example.com</a></i><br />\
  <b>Tags</b>: <i>foo bar baz</i>\
  \
  <p>This is a long paragraph of the description. Normally it would be generated using dieu.database.get_post(), but I think that for the purposes of just trying out whether the whole system works, this suffices.</p>\
  </body>\
</html>"
        
        self.database = dieu.database.DieuDatabase(username, password)
        
        self.window = gtk.Window()
        self.window.set_title(username+"'s bookmarks")
        self.window.set_icon(gtk.gdk.pixbuf_new_from_file('@prefix@/share/pixmaps/dieu/dieu.svg'))
        self.window_table = gtk.Table(3, 4)
        self.window.add(self.window_table)
        
        self.window_menus = gtk.MenuBar()
        self.window_table.attach(self.window_menus, 0, 3, 0, 1, gtk.FILL, gtk.FILL)
        
        self.item_Bookmarks = gtk.MenuItem('Bookmarks')
        self.window_menus.append(self.item_Bookmarks)
        
        self.menu_Bookmarks = gtk.Menu()
        self.item_Bookmarks.set_submenu(self.menu_Bookmarks)
        
        self.item_Bookmarks_Add = gtk.ImageMenuItem('Add bookmark')
        self.menu_Bookmarks.append(self.item_Bookmarks_Add)
        
        self.item_Bookmarks_Close = gtk.ImageMenuItem('Close')
        self.menu_Bookmarks.append(self.item_Bookmarks_Close)
        
        self.item_Edit = gtk.MenuItem('Edit')
        self.window_menus.append(self.item_Edit)
        
        self.item_Help = gtk.MenuItem('Help')
        self.window_menus.append(self.item_Help)
        
        self.window_toolbar = gtk.Toolbar()
        self.window_table.attach(self.window_toolbar, 0, 2, 1, 2, gtk.FILL, gtk.FILL)
        
        self.toolitem_Add = gtk.ToolButton()
        self.toolitem_Add.set_icon_name('bookmark-new')
        self.toolitem_Add.connect('clicked', lambda w: dieu.gui.add.DieuAddPost())
        self.window_toolbar.insert(self.toolitem_Add, -1)
        
        self.toolitem_Remove = gtk.ToolButton(gtk.STOCK_DELETE)
        self.window_toolbar.insert(self.toolitem_Remove, -1)
        
        self.toolitem_Edit = gtk.ToolButton(gtk.STOCK_EDIT)
        self.window_toolbar.insert(self.toolitem_Edit, -1)
        
        self.toolitem_separator_1 = gtk.SeparatorToolItem()
        self.window_toolbar.insert(self.toolitem_separator_1, -1)
        
        self.toolitem_Refresh = gtk.ToolButton(gtk.STOCK_REFRESH)
        self.window_toolbar.insert(self.toolitem_Refresh, -1)
        
        self.toolitem_separator_2 = gtk.SeparatorToolItem()
        self.window_toolbar.insert(self.toolitem_separator_2, -1)
        
        self.toolitem_Search = gtk.ToolItem()
        self.toolitem_hbox = gtk.HBox()
        self.toolitem_label_Search = gtk.Label('Search:')
        self.toolitem_hbox.pack_start(self.toolitem_label_Search)
        self.toolitem_entry_Search = gtk.Entry()
        self.toolitem_hbox.pack_start(self.toolitem_entry_Search)
        self.toolitem_Search.add(self.toolitem_hbox)
        self.window_toolbar.insert(self.toolitem_Search, -1)
        
        self.sidebar_scrolledwindow = gtk.ScrolledWindow()
        self.sidebar_scrolledwindow.set_shadow_type(gtk.SHADOW_IN)
        self.sidebar_scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
        
        self.sidebar_treeview_model = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
        self.sidebar_treeview = gtk.TreeView(self.sidebar_treeview_model)
        self.sidebar_treeview_renderer = gtk.CellRendererText()
        self.sidebar_treeview_column = gtk.TreeViewColumn('Tag', self.sidebar_treeview_renderer, text=1)
        self.sidebar_treeview.append_column(self.sidebar_treeview_column)
        self.sidebar_treeview.set_size_request(150, 400)
        
        self.sidebar_treeview_model.append([gobject.TYPE_STRING, 'All tags'])
        
        for tag in self.database.get_all_tags('Alphabetical'):
            if tag[1] != '':
                self.sidebar_treeview_model.append([gobject.TYPE_STRING, tag[1]])
        
        self.sidebar_scrolledwindow.add(self.sidebar_treeview)
        
        self.window_table.attach(self.sidebar_scrolledwindow, 0, 1, 2, 4, gtk.FILL)
        
        self.main_scrolledwindow = gtk.ScrolledWindow()
        self.main_scrolledwindow.set_shadow_type(gtk.SHADOW_IN)
        self.main_scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
        
        self.main_treeview_model = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
        self.main_treeview = gtk.TreeView(self.main_treeview_model)
        self.main_treeview_renderer = gtk.CellRendererText()
        self.main_treeview_column = gtk.TreeViewColumn('Num', self.main_treeview_renderer, text=1)
        self.main_treeview.append_column(self.main_treeview_column)
        self.main_treeview_renderer_Desc = gtk.CellRendererText()
        self.main_treeview_column_Desc = gtk.TreeViewColumn('Title', self.main_treeview_renderer_Desc, text=2)
        self.main_treeview.append_column(self.main_treeview_column_Desc)
        self.main_treeview.set_size_request(500, 400)
        
        for bookmark in self.database.get_all_posts('Default'):
            row = self.main_treeview_model.append([(gobject.TYPE_STRING, bookmark[0]), (gobject.TYPE_STRING, bookmark[1])])
            self.main_treeview_model.set(row, 1, bookmark[0])
            self.main_treeview_model.set(row, 0, bookmark[1])
        
        self.main_scrolledwindow.add(self.main_treeview)
        
        self.window_table.attach(self.main_scrolledwindow, 1, 2, 2, 3)
        
        self.desc_scrolledwindow = gtk.ScrolledWindow()
        self.desc_scrolledwindow.set_shadow_type(gtk.SHADOW_IN)
        
        self.desc_textbuffer = gtk.TextBuffer()
        self.desc_view = gtk.TextView(self.desc_textbuffer)
        self.desc_view.set_wrap_mode(gtk.WRAP_WORD)
        
        self.desc_document = gtkhtml2.Document()
        self.desc_document.open_stream('text/html')
        self.desc_document.write_stream(self.page)
        self.desc_document.close_stream()
        self.desc_view = gtkhtml2.View()
        self.desc_view.set_document(self.desc_document)
        self.desc_document.connect('link_clicked', self.url_action)
        
        self.desc_scrolledwindow.add(self.desc_view)
        self.desc_scrolledwindow.set_size_request(500, 150)
        self.desc_scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        
        self.window_table.attach(self.desc_scrolledwindow, 1, 2, 3, 4)
        
        self.desc_view.show()
        self.desc_scrolledwindow.show()
        self.main_treeview.show()
        self.main_scrolledwindow.show()
        self.sidebar_treeview.show()
        self.sidebar_scrolledwindow.show()
        self.toolitem_entry_Search.show()
        self.toolitem_label_Search.show()
        self.toolitem_Search.show()
        self.toolitem_hbox.show()
        self.toolitem_separator_2.show()
        self.toolitem_Refresh.show()
        self.toolitem_separator_1.show()
        self.toolitem_Edit.show()
        self.toolitem_Remove.show()
        self.toolitem_Add.show()
        self.window_toolbar.show()
        self.item_Help.show()
        self.item_Edit.show()
        self.item_Bookmarks_Close.show()
        self.item_Bookmarks_Add.show()
        self.item_Bookmarks.show()
        self.window_menus.show()
        self.window_table.show()
        self.window.show()
    
    def url_action(self, document, link):
        webbrowser.open(link)
